<?php
//wx3464c6b9fd10bf5c   appid

include_once "wxBizMsgCrypt.php";
//file_put_contents('./log/4.log', json_encode($_GET), FILE_APPEND);

// 第三方发送消息给公众平台
$encodingAesKey = "kAY0ar3xv6RAX8EdgUIaSBBDLEuKjAYeVi6WweAxWh4";
$token = "7a616f4a0af798d6011494843e2fc78f";
$appId = "wx82aa345f1fb8714b";
$timeStamp  = empty($_GET['timestamp']) ? "" : trim($_GET['timestamp']) ;
$nonce      = empty($_GET['nonce']) ? "" : trim($_GET['nonce']) ;
$msg_sign   = empty($_GET['msg_signature']) ? ""    : trim($_GET['msg_signature']) ;

$encryptMsg = file_get_contents('php://input');
$pc = new WXBizMsgCrypt($token, $encodingAesKey, $appId);

$xml_tree = new DOMDocument();
$xml_tree->loadXML($encryptMsg);
$array_e = $xml_tree->getElementsByTagName('Encrypt');
$encrypt = $array_e->item(0)->nodeValue;
$format = "<xml><ToUserName><![CDATA[toUser]]></ToUserName><Encrypt><![CDATA[%s]]></Encrypt></xml>";
$from_xml = sprintf($format, $encrypt);
//logResult(1,$from_xml);

$msg = '';
$errCode = $pc->decryptMsg($msg_sign, $timeStamp, $nonce, $from_xml, $msg);
$component_verify_ticket ="";
if ($errCode == 0) {
    
    $xml = new DOMDocument();
    $xml->loadXML($msg);
    $textType = $xml->getElementsByTagName('MsgType');
    $fromUser=$xml->getElementsByTagName('FromUserName');
    $openid=$fromUser->item(0)->nodeValue;
    $type = $textType->item(0)->nodeValue;
    $textContent=$xml->getElementsByTagName('Content');
    $content=$textContent->item(0)->nodeValue;
    if('event' == $type){
    	$eventTag=$xml->getElementsByTagName('Event');
    	$eventName = $eventTag->item(0)->nodeValue;
    	logResult(4,$msg);
    	logResult(5,$eventName);
    }
    $sendtext='';
    $time=time();
    if(123456789 == $content){
        $sendtext='123456789s';
        logResult(4,$openid);
        $msg = '';
        $text='<xml>
  <ToUserName><![CDATA['.$openid.']]></ToUserName>
  <FromUserName><![CDATA[gh_c5876c482fcd]]></FromUserName>
  <CreateTime>'.$time.'</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA['.$sendtext.']]></Content>
</xml>';
        $pc->encryptMsg($text, $timeStamp, $nonce, $msg);
        //echo 'success';
        
        echo $msg;
    }
    
    if(1234 == $content){
    	  
    	$appid='wxdf0eee3a5e4ad06a';
    	$secet='Ih5QhYYETTBqf7uANRkYX5LEW2RvtW';
    	 
    	$url='https://onex002.onex.im/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secet;
    	$getToken=json_decode(file_get_contents($url),true);
    	$thisToken=$getToken['access_token'];

    	$url='https://onex002.onex.im/cgi-bin/message/custom/send?access_token='.$thisToken;
    	$data='{
		  "touser":"'.$openid.'",
		  "msgtype":"wxcard",
		  "wxcard":
		  {
		   "card_id":"pj20C1V059UNmA6UQ8w4OwT4r6jI"
		   }
		}';
    	$res = json_decode(http_request($url, $data), true);
    	
    	 
    }
    
    
    exit;
    
    
    //logResult(3,$type.':'.$content.$text);
    echo $text;
}else {
    logResult(4,'解密后失败：'.$errCode);
    print($errCode . "\n");
}


function logResult($file,$data){
    file_put_contents('./log/'.$file.'.log', $data,FILE_APPEND);
}


function http_request($url, $data = null)
{
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_URL, $url);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	if (!empty($data)){
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	}
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
	$output = curl_exec($curl);
	curl_close($curl);
	return $output;
}




 

